package com.frxuexi.mdm_2207.view

import android.util.Log
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.ElevatedCard
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import coil3.compose.AsyncImage
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.lib_base.R
import com.frxuexi.lib_base.RoutePath
import com.frxuexi.lib_base.UiState
import com.frxuexi.lib_base.room.User
import com.frxuexi.mdm_2207.MainActivity
import com.frxuexi.mdm_2207.activity
import com.frxuexi.myroom.intent.UserIntent
import com.frxuexi.myroom.viewmodel.UserViewModel
import com.tencent.mmkv.MMKV
import com.wildma.pictureselector.PictureSelector
import kotlinx.coroutines.launch

var url by mutableStateOf("https://img1.baidu.com/it/u=4035546522,389745751&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1735146000&t=a6cb452f25f5456854d0cc65889449b6")
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun MyPage(navHostController: NavHostController,vm:UserViewModel= hiltViewModel()) {
    val kv = MMKV.defaultMMKV()
    var uname by remember {
        mutableStateOf(kv.decodeString("uname", "未登录")!!)
    }
    var role by remember {
        mutableStateOf(kv.decodeString("role","")!!)
    }
    var isShow by remember {
        mutableStateOf(false)
    }

    var showLogin by remember {
        mutableStateOf(false)
    }

//    var myurl by remember {
//        mutableStateOf(url)
//    }
    var list = remember {
        SnapshotStateList<User>()
    }

    LaunchedEffect(key1 = "", block = {
        launch {
            vm.state.collect {
                when(it){
                    is UiState.onSuccess<*> ->{
                        list.clear()
                        Log.i("===",it.data.toString())
                        list.addAll(it.data as Collection<User>)
                    }

                    else -> {}
                }
            }
        }
    })


    Column(
        Modifier
            .fillMaxSize()
            .padding(20.dp),
        verticalArrangement = Arrangement.spacedBy(10.dp)
    ) {
        Row (Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceBetween){
            Text(text = "我的", fontSize = 28.sp, fontWeight = FontWeight.Bold)
            Text(text = "切换账号",Modifier.clickable {
                showLogin=true
                vm.sendIntent(UserIntent.getLogin)
            })
        }

        ElevatedCard(colors= CardDefaults.cardColors(containerColor = Color.White)) {
            Row(modifier = Modifier
                .fillMaxWidth()
                .padding(20.dp, 10.dp)) {
                AsyncImage(
                    model = url,
                    contentDescription = "",
                    modifier = Modifier
                        .size(80.dp)
                        .clip(CircleShape)
                        .clickable {
                                   PictureSelector.create(activity,PictureSelector.SELECT_REQUEST_CODE).selectPicture(true,80,80,1,1)
                        },
                    contentScale = ContentScale.Crop
                )
                Spacer(modifier = Modifier.width(10.dp))
                Column(modifier = Modifier.height(80.dp),verticalArrangement = Arrangement.SpaceAround) {
                    Text(text = uname, modifier = Modifier.clickable {
                        if (uname == "未登录")
                            navHostController.navigate(RoutePath.ROOM_LOGIN)
                        else {
                            isShow = true
                        }
                    })
                    Text(text = role)
                }

            }
        }

        Text(text = "常用功能")
        ElevatedCard(colors= CardDefaults.cardColors(containerColor = Color.White)) {
            Row(modifier = Modifier
                .fillMaxWidth()
                .padding(20.dp, 10.dp)) {
                Column(Modifier.clickable {
                    if (uname == "未登录"){
                        ToastUtils.showLong("还未登录")
                    }else{
                        navHostController.navigate(RoutePath.ROOM_LEAVE_LIST)
                    }
                }) {
                    Image(painter = painterResource(id = R.drawable.icon359), contentDescription = "",Modifier.size(30.dp))
                    Spacer(modifier = Modifier.height(8.dp))
                    Text(text = "请假")
                }
                Spacer(modifier = Modifier.width(20.dp))
                if(role=="经理") {
                    Column(Modifier.clickable {
                        if (uname == "未登录") {
                            ToastUtils.showLong("还未登录")
                        } else {
                            navHostController.navigate(RoutePath.ROOM_APPROVE_LEAVE_LIST)
                        }
                    }) {
                        Image(
                            painter = painterResource(id = R.drawable.icon81),
                            contentDescription = "",
                            Modifier.size(30.dp)
                        )
                        Spacer(modifier = Modifier.height(8.dp))
                        Text(text = "审批")
                    }
                }
            }
        }
    }

    if(isShow){
        AlertDialog(onDismissRequest = { isShow=false }, confirmButton = {
            Text(text = "确认",Modifier.clickable {
                kv.remove("uname")
                kv.remove("role")
                uname = "未登录"
                role=""
                isShow=false
            })
        }, title = {
            Text(text = "退出提醒")
        }, text = {
            Text(text = "是否要退出当前登录？")
        })
    }

    if(showLogin){
        AlertDialog(onDismissRequest = { showLogin = false }, modifier = Modifier
            .fillMaxWidth()
            .background(Color.White, RoundedCornerShape(20.dp))
            .padding(20.dp)) {
            LazyColumn(verticalArrangement = Arrangement.spacedBy(10.dp), content = {
                items(list){
                    Row(modifier = Modifier
                        .fillMaxWidth()
                        .background(Color.Yellow, RoundedCornerShape(20.dp))
                        .padding(20.dp, 10.dp)
                        .clickable {
                            kv.encode("uname", it.uname)
                            kv.encode("role", it.role)
                            uname = it.uname
                            role = it.role
                            showLogin = false
                        }, verticalAlignment = Alignment.CenterVertically,
                        horizontalArrangement = Arrangement.SpaceBetween) {
                        Text(text = it.uname)
                        if(it.uname==uname){
                            Image(painter = painterResource(id = com.frxuexi.myroom.R.drawable.ic_action_logined), contentDescription = "")
                        }
                    }

                }
            })
        }
    }

}